/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-06-15
 * Time: 20:20
 */
//二分算法 x的平方根
//时间复杂度O(logn)
public class Solution {
    public int mySqrt(int x) {
        //细节
        if (x < 1) {
            return 0;
        }

        //数据范围不能越界 mid * mid 所以用long
        long left = 1;
        long right = x;

        while (left < right) {
            long mid = left + (right - left + 1) / 2;

            if (mid * mid <= x) {
                left = mid;
            }else {
                right = mid - 1;
            }
        }

        return (int)left;
    }
}
